package com.ylr.admin.config;

import com.ylr.base.framework.service.impl.AsyncServiceImpl;
import com.ylr.base.common.utils.ThrottleUtils;
import com.ylr.base.framework.service.AsyncService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-04-21 13:37:40
 * className: YlrAdminConfig
 * version: 1.0
 * description:
 *  `@EnableDiscoveryClient`: 服务注册到nacos
 */
@Configuration
@EnableDiscoveryClient
public class YlrAdminConfig {

    @Value("${spring.application.simple-name}")
    private String applicationSimpleName;

    /**
     * 注入节流执行工具类对象
     * @param scheduledThreadPoolExecutor 延迟线程池
     * @return 类对象
     */
    @Bean
    public ThrottleUtils throttleUtils(ScheduledThreadPoolExecutor scheduledThreadPoolExecutor) {
        return new ThrottleUtils(scheduledThreadPoolExecutor);
    }

    /**
     * 注入异步任务服务类对象
     * @param threadPoolTaskExecutor      异步线程池
     * @param scheduledThreadPoolExecutor 延迟线程池
     * @return 异步任务服务对象
     */
    @Bean
    public AsyncService asyncService(ThreadPoolTaskExecutor threadPoolTaskExecutor, ScheduledThreadPoolExecutor scheduledThreadPoolExecutor) {
        return new AsyncServiceImpl(applicationSimpleName, threadPoolTaskExecutor, scheduledThreadPoolExecutor);
    }

}
